﻿using BaseUtils.Helper;
using Newtonsoft.Json;
using NWMES;
using NWMES.Mes;
using NWMES.MiFindCustomAndSfcDataServiceService;
using System;
using System.Net;

namespace MyMes.Mes
{
    /// <summary>
    /// http://ndmes7.catlbattery.com:8103/atlmeswebservice/MiFindCustomAndSfcDataServiceService?wsdl
    /// 获取PN 
    /// 常用在进站等场景
    /// 
    /// </summary>
    public class MiFindCustomAndSfcData : MESBaseService
    {
        static string DefaultApiSection = "MiFindCustomAndSfcData";
        public static NetworkCredential networkCredential = null;
        public static MiFindCustomAndSfcData Params = null;

        static MiFindCustomAndSfcData()
        {
            Params = config[DefaultApiSection].ToObject<MiFindCustomAndSfcData>();
            networkCredential = new NetworkCredential(Params.UserName, Params.Password);
        }

        /// <summary>
        /// 
        /// 获取PN 
        /// 常用在进站等场景
        /// 
        /// </summary>
        /// <param name="sfc">条码;如模组码，pack码，电芯码的物料码</param>
        /// <param name="inventory">客户码</param>
        /// <returns></returns>
        public static MesResult RequestMes(string sfc, string inventory,
            string ApiSection)
        {
            if (!string.IsNullOrEmpty(ApiSection))
            {
                Params = config[ApiSection].ToObject<MiFindCustomAndSfcData>();
                networkCredential = new NetworkCredential(Params.UserName, Params.Password);
            }
            else
            {
                ApiSection = DefaultApiSection;
            }
            string ErrorMessage = Params.ParameterVerification();
            if (!string.IsNullOrEmpty(ErrorMessage))
            {
                return new MesResult(-99, ErrorMessage);
            }
            MiFindCustomAndSfcDataServiceService miFindCustomAndSfcDataServiceService;
            findCustomAndSfcDataRequest FindCustomAndSfcDataRequest;
            SetRequestParams(sfc, inventory, out miFindCustomAndSfcDataServiceService, out FindCustomAndSfcDataRequest);

            miFindCustomAndSfcDataResponse Response = new miFindCustomAndSfcDataResponse();
            miFindCustomAndSfcData MiFindCustomAndSfcData = new miFindCustomAndSfcData();
            MiFindCustomAndSfcData.FindCustomAndSfcDataRequest = FindCustomAndSfcDataRequest;

            MesResult result = MesResult.InitMesResult(JsonConvert.SerializeObject(MiFindCustomAndSfcData));
            try
            {
                Response = miFindCustomAndSfcDataServiceService.miFindCustomAndSfcData(MiFindCustomAndSfcData);

                result.EndTime = DateTime.Now;
                var ret = Response.@return;
                result.Message = ret?.message;
                result.Code = ret.code;
                result.Success = result.Code == 0;
                result.ResponeJson = JsonConvert.SerializeObject(Response);

                result.Respone = Response;
                result.ret = ret;
            }
            catch (Exception ex)
            {
                result.Code = -99;
                result.Message = ex.Message;
            }

            SaveMesLog(ApiSection, result);
            return result;
        }

        private static void SetRequestParams(string sfc, string inventory, out MiFindCustomAndSfcDataServiceService miFindCustomAndSfcDataServiceService, out findCustomAndSfcDataRequest FindCustomAndSfcDataRequest)
        {
            miFindCustomAndSfcDataServiceService = new MiFindCustomAndSfcDataServiceService();
            miFindCustomAndSfcDataServiceService.Url = Params.Url;
            miFindCustomAndSfcDataServiceService.Timeout = Params.Timeout;
            miFindCustomAndSfcDataServiceService.Credentials = networkCredential;
            miFindCustomAndSfcDataServiceService.PreAuthenticate = true;

            FindCustomAndSfcDataRequest = new findCustomAndSfcDataRequest();
            FindCustomAndSfcDataRequest.site = Params.Site;
            FindCustomAndSfcDataRequest.user = Params.User;
            FindCustomAndSfcDataRequest.operation = Params.Operation;
            FindCustomAndSfcDataRequest.operationRevision = Params.OperationRevision;
            FindCustomAndSfcDataRequest.activity = Params.ActivityId;
            FindCustomAndSfcDataRequest.resource = Params.Resource;
            ObjectAliasEnum[] aliasArray = new ObjectAliasEnum[1]{
                        ObjectAliasEnum.ITEM
                    };
            FindCustomAndSfcDataRequest.masterDataArray = aliasArray;
            FindCustomAndSfcDataRequest.customDataArray = new customDataInParametricData[1]
                {
                            new customDataInParametricData() {
                                category = ObjectAliasEnum.ITEM,
                                dataField = "XPXS"
                        },
            };

            FindCustomAndSfcDataRequest.findSfcByInventory = Params.findSfcByInventory;//true为PN,false为模组码
            FindCustomAndSfcDataRequest.modeProcessSFC = ConvertHelper.ToEnum<modeProcessSFC>(Params.ModeProcessSFC);
            FindCustomAndSfcDataRequest.sfcOrder = Params.sfcOrder;
            FindCustomAndSfcDataRequest.targetOrder = Params.targetOrder;

            //传入sfc
            FindCustomAndSfcDataRequest.sfc = sfc;
            if (inventory != null)
            {
                FindCustomAndSfcDataRequest.inventory = inventory;
            }
        }

        public bool findSfcByInventory { get; set; }

        /// <summary>
        /// SFC工单 （主工单）
        /// </summary>
        public string sfcOrder { get; set; }

        /// <summary>
        /// 目标工单 （从工单）
        /// </summary>
        public string targetOrder { get; private set; } = "";

        /// <summary>
        /// 客户码
        /// </summary>
        public string inventory { get; set; }
    }
}
